<?php

namespace app\admin\controller;

use app\admin\model\AuthGroup;
use app\admin\model\User;
use app\common\controller\Backend;
use fast\Random;
use think\Db;
use think\Validate;

/**
 * 系统消息
 *
 * @icon fa fa-circle-o
 */
class Systemmessage extends Backend
{

    /**
     * SystemMessage模型对象
     * @var \app\admin\model\SystemMessage
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\SystemMessage;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    public function add()
    {
        if ($this->request->isPost()){
            $params = $this->request->post("row/a", []);

            if ($params) {
                Db::startTrans();
                try {

                    $result = $this->model->save($params);

                    if ($result === false) {
                        $this->error($this->model->getError());
                    }
                    if ($params['type']==0){
                        //发送用户
                        $list = User::column('id');
                        $user_message_model = new \app\admin\model\UserMessage();
                        $message_data = [];
                        foreach ($list as $k=>$v){
                            $message_data[] = [
                                'user_id'=>$v,
                                'message_id'=>$this->model->id,
                                'title'=>$params['title'],
                                //'image'=>$params['image'],
                                'intro'=>$params['intro'],
                                'content'=>$params['content']
                            ];

                        }
                        $user_message_model->saveAll($message_data);
                    } else {
                        //发送商家
                        $list = \app\admin\model\Shop::column('id');
                        $shop_message_model = new \app\admin\model\ShopMessage();
                        $message_data = [];
                        foreach ($list as $k=>$v){
                            $message_data[] = [
                                'shop_id'=>$v,
                                'message_id'=>$this->model->id,
                                'title'=>$params['title'],
                                //'image'=>$params['image'],
                                'intro'=>$params['intro'],
                                'content'=>$params['content']
                            ];

                        }
                        $shop_message_model->saveAll($message_data);
                    }

                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error();
        }

        return $this->view->fetch();
    }
}
